import numpy as np
from scipy import integrate
import pandas as pd
from datetime import datetime


class Rows:
    def __init__(self):
        aa = 0

    def avg(self, reg, window_size, total_size):
        def f_p(*args):
            return reg.predict([[args[0]]])[0]

        result_list = []
        start = datetime.now()
        total_min = 0
        total_max = total_size - 1

        row_min = 0
        row_max = 0
        ans = float(0)

        for count in range(total_max):
            pre_row_min = row_min
            pre_row_max = row_max
            row_min = count - window_size
            if row_min < total_min:
                row_min = total_min
            row_max = count + window_size
            if row_max > total_max:
                row_max = total_max

            if count % 10000 == 0:
                ans = integrate.quad(f_p, row_min, row_max, epsabs=10.0, epsrel=0.1)[0] / (row_max - row_min + 1)
            else:
                row_min = pre_row_min
                row_max = pre_row_max

            result_list.append(ans)

        end = datetime.now()
        time_cost = (end - start).total_seconds()
        print("time cost {}".format(time_cost))
        result_list = np.array(result_list)
        return result_list

    def sum(self, reg, window_size, total_size):
        def f_p(*args):
            return reg.predict([[args[0]]])[0]

        result_list = []
        start = datetime.now()
        total_min = 0
        total_max = total_size - 1

        row_min = 0
        row_max = 0
        ans = float(0)

        for count in range(total_max):
            pre_row_min = row_min
            pre_row_max = row_max
            row_min = count - window_size
            if row_min < total_min:
                row_min = total_min
            row_max = count + window_size
            if row_max > total_max:
                row_max = total_max

            if count % 10000 == 0:
                ans = integrate.quad(f_p, row_min, row_max, epsabs=10.0, epsrel=0.1)[0] / (row_max - row_min + 1)
                ans = ans * (window_size * 2 + 1)
            else:
                row_min = pre_row_min
                row_max = pre_row_max

            result_list.append(ans)

        end = datetime.now()
        time_cost = (end - start).total_seconds()
        print("time cost {}".format(time_cost))
        result_list = np.array(result_list)
        return result_list
